package com.journey.org.ui.home.beauty.dayline

import android.app.Application
import androidx.databinding.ObservableArrayList
import androidx.databinding.ObservableField
import androidx.databinding.ObservableInt
import androidx.lifecycle.viewModelScope
import com.journey.org.BR
import com.journey.org.R
import com.journey.org.app.HttpManager
import com.mvvmhabit.org.base.BaseViewModel
import com.mvvmhabit.org.binding.command.BindingAction
import com.mvvmhabit.org.binding.command.BindingCommand
import com.mvvmhabit.org.custom.LoadingLayout
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.cancel
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import me.tatarka.bindingcollectionadapter2.ItemBinding

/**
 * @Date: 2020/11/24 16:26
 * @Author:小妖
 * @Description: 天行
 */
class DayLineViewModel(application: Application) : BaseViewModel(application) {

    val requestStateField = ObservableInt()

    val onClickAgainLoadCommand = BindingCommand<Any>(object : BindingAction {
        override fun call() {
            page = 1
            loadBeautyGirlList()
        }
    })

    var page = 1

    val onSearchField = ObservableField<String>("")

    val onRefreshCommand = BindingCommand<Any>(object : BindingAction {
        override fun call() {
            page = 1
            loadBeautyGirlList()
        }
    })

    val onLoadMoreCommand = BindingCommand<Any>(object : BindingAction {
        override fun call() {
            ++page
            loadBeautyGirlList()
        }
    })


    val itemBinding =
        ItemBinding.of<DayLineItemViewModel>(BR.viewModel, R.layout.day_line_item)

    val items = ObservableArrayList<DayLineItemViewModel>()

    fun loadBeautyGirlList() {
        if (page == 1) {
            requestStateField.set(LoadingLayout.NETWORK_LOADING)
            items.clear()
        }

    }
}